<template>
	<image class="load" src="/static/loading.gif" v-if="isload"></image>
	<view class="container">
		<view class="module">
			<image src="/static/baby.svg"></image>
			<text>孩子</text>
			<button type="primary" @click="selectUserBaby">选择</button>
		</view>

		<view class="module">
			<image src="/static/patriarch.svg"></image>
			<text>家长</text>
			<view>
				<button type="primary" @click="selectUserPatriarch">选择</button>
			</view>
		</view>
	</view>

</template>

<script>
	import {
		updateUserInfo
	} from '@/api/api.js'
	import store from '@/store/index.js'; //需要引入store
	export default {
		data() {
			return {
				userInfo: {
					userType: 0,
					isload: false
				}
			}
		},
		created() {
			var that = this
			this.$watch('userInfo.userType', (newValue, oldValue) => {
				console.log(`userInfo 的 userType 属性从 ${oldValue} 变为 ${newValue}`);
				updateUserInfo(that.userInfo).then(res => {
					console.log(res.data)
					if (res.data.code != 0) {
						uni.showToast({
							icon: "none",
							title: res.data.message
						})

					} else {

						uni.showToast({
							icon: "none",
							title: "修改成功"
						})
						uni.redirectTo({
							url: '/pages/user/user_info'
						});

					}
					that.isload = false
				})

			});
		},
		methods: {

			selectUserBaby() {
				this.isload = true
				store.commit('myMutation', 2)
				this.userInfo.userType = 2
			},
			selectUserPatriarch() {
				this.isload = true
				store.commit('myMutation', 1)
				this.userInfo.userType = 1
			},
		},
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
	}

	.module {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding: 20px;
		border: 1px solid #ccc;
		border-radius: 5px;
	}

	image {
		width: 100px;
		height: 100px;
		border-radius: 50%;
		margin-bottom: 10px;
	}

	button {
		text-align: center;
		font-size: 30rpx;
		width: 150rpx;
		height: 70rpx;
	}

	.load {
		display: flex;
		position: absolute;
		margin-top: 350rpx;
		z-index: 3;
		margin-left: 70rpx;
	}
</style>